package com.room.search.service;

import com.room.search.constant.UserConstants;
import com.room.search.core.page.TableDataInfo;
import com.room.search.domain.RoomDictType;
import com.room.search.repository.RoomDictTypeRepository;
import com.room.search.response.RoomResult;
import com.room.search.security.SecurityUtils;
import com.room.search.service.dto.RoomDictTypeDTO;
import com.room.search.service.mapper.RoomDictTypeMapper;
import com.room.search.util.PageUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * By   : ghd.bright
 * Time : 2022/3/3
 * Description:
 */

@Service
public class RoomDictTypeService {

    private final RoomDictTypeRepository roomDictTypeRepository;
    private final RoomDictTypeMapper roomDictTypeMapper;

    public RoomDictTypeService(RoomDictTypeRepository roomDictTypeRepository, RoomDictTypeMapper roomDictTypeMapper) {
        this.roomDictTypeRepository = roomDictTypeRepository;
        this.roomDictTypeMapper = roomDictTypeMapper;
    }

    /**
     * 分页查询所有的dicType
     */
    public TableDataInfo getAllDictType(RoomDictTypeDTO dto) {
        Pageable pageable = PageUtils.startPage();
        Page<RoomDictType> all = roomDictTypeRepository.findAll((root, query, c) -> {
            List<Predicate> predicates = new ArrayList<>();

            if (dto.getDictName() != null) {
                predicates.add(c.equal(root.get("dict_name"), dto.getDictName()));
            }

            if (dto.getDictType() != null) {
                predicates.add(c.equal(root.get("dict_type"), dto.getDictType()));
            }

            if (dto.getStatus() != null) {
                predicates.add(c.equal(root.get("status"), dto.getStatus()));
            }

            return query.where(predicates.toArray(new Predicate[0])).getRestriction();
        }, pageable);
        return TableDataInfo.getTableDataInfo(all);
    }

    /**
     * 根据字典类型ID查询信息
     */
    public RoomDictTypeDTO getInfoByDictCode(Long dictId) {
        return roomDictTypeRepository.findById(dictId).map(roomDictTypeMapper::toDto).orElse(null);
    }

    /**
     *新增字典类型
     */
    public RoomResult add(RoomDictTypeDTO dto) {
        RoomDictType roomDictType = roomDictTypeMapper.toEntity(dto);
        if (UserConstants.NOT_UNIQUE.equals(checkDictTypeUnique(roomDictType.getDictType()))) {
            return RoomResult.error("新增字典'" + dto.getDictName() + "'失败，字典类型已存在");
        }
        String createBy = SecurityUtils.getCurrentUserLogin().orElse("admin");
        roomDictType.setCreateBy(createBy);
        roomDictType.setCreateTime(new Date());
        roomDictTypeRepository.save(roomDictType);
        return RoomResult.success("新增字典成功");
    }


    /**
     * 修改字典
     */
    public RoomResult edit(RoomDictTypeDTO dto) {
        RoomDictType roomDictType = roomDictTypeMapper.toEntity(dto);
        if (UserConstants.NOT_UNIQUE.equals(checkDictTypeUnique(roomDictType.getDictType()))) {
            return RoomResult.error("修改字典'" + dto.getDictName() + "'失败，字典类型已存在");
        }
        String updateBy = SecurityUtils.getCurrentUserLogin().orElse("admin");
        roomDictType.setUpdateTime(new Date());
        roomDictType.setUpdateBy(updateBy);
        roomDictTypeRepository.save(roomDictType);
        return RoomResult.success("此u该成功");
    }


    //判断字典是否被拥有
    public String checkDictTypeUnique(String dictType) {
        RoomDictType roomDictType = roomDictTypeRepository.findAllByDictType(dictType).orElse(null);
        if (roomDictType != null) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 批量删除字典
     */
    public RoomResult deleteDictTypeByIds(Long[] dictIds) {
        roomDictTypeRepository.deleteAllByIdInBatch(Arrays.asList(dictIds.clone()));
        return RoomResult.success("删除成功");
    }

    /**
     * 刷新字典缓存
     */
    public RoomResult resetDictCache() {
        return null;
    }

    /**
     *获取字典选择框列表
     */
    public RoomResult selectDictTypeAll() {
        return null;
    }
}
